﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SmartSpace.Core;
using SmartSapce.ServiceLocation;
using System.IO;

namespace SmartSpace.WebApp._console.core
{
    public partial class application_list : System.Web.UI.Page
    {
        private Guid _selectedApplicationId;
        private List<SmartSpace.Core.Module> _selectedApplicationModules;
        private Guid _selectedApplicationThemeId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                _selectedApplicationId = new Guid(Request.QueryString["id"]);

            }

            if (!IsPostBack)
            {
                ICoreService service = ServiceLocator.Get<ICoreService>();
                List<Site> apps = service.GetSites();
                _ctlList.DataSource = apps;
                _ctlList.DataBind();

                if (_selectedApplicationId != Guid.Empty)
                {
                    SmartSpace.Core.Site application = service.GetSite(_selectedApplicationId);
                }
            }
        }

        public string GetThemeName(object themeId)
        {
            if (themeId != null)
            {
                Guid id = new Guid(themeId.ToString());
                ICoreService service = ServiceLocator.Get<ICoreService>();
                SiteTemplate theme = service.GetSiteTemplate(id);
                if (theme != null)
                {
                    return theme.Caption;
                }
            }
            return "none";
        }

        #region 应用程序相关设置
        public bool IsSelected(object hostUrl)
        {
            Uri hostUri = new Uri(hostUrl.ToString());

            return hostUri.IsBaseOf(Request.Url);
        }

        public bool IsCurrentTheme(object themeId)
        {
            if (_selectedApplicationThemeId == (Guid)themeId)
            {
                return true;
            }
            return false;
        }

        #endregion

        protected void _ctlCmdDelete_Click(object sender, EventArgs e)
        {
            LinkButton btn = sender as LinkButton;
            Guid id = new Guid(btn.CommandArgument);
            ICoreService service = ServiceLocator.Get<ICoreService>();
            if (service.DeleteSite(id))
            {
                Response.Redirect(Request.Url.AbsoluteUri);
            }
            else
            {
                Response.Redirect(Request.Url.AbsoluteUri);
            }
        }
    }
}